package com.tjyx.maintain.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.tjyx.common.TJYXException;
import com.tjyx.common.action.BaseAction;
import com.tjyx.common.util.DTOUtil;
import com.tjyx.common.util.constants.TableNameConstants;
import com.tjyx.maintain.bo.UserInfoBo;
import com.tjyx.maintain.dao.UserInfoDao;
import com.tjyx.maintain.dto.UserInfo;

/**
 * 用户管理转发控制Action<br />
 * 时间：2012-3-26
 * 
 * @author zhutianjie
 * @version v1.0
 *
 */
public class UserInfoAction extends BaseAction {

	/*
	 * 员工管理业务逻辑对象
	 */
	private UserInfoBo userInfoBo = new UserInfoBo();
	/*
	 * 员工管理数据访问对象
	 */
	private UserInfoDao userInfoDao = new UserInfoDao();

	/**
	 * 查询管理员信息列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String managerList(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		initPagination(request);
		String json;
		try {
			json = userInfoBo.getPaginationJSON(page, rows, UserInfo.class,
					TableNameConstants.MAINTAIN_USERINFO);
			write2View(json, response);
		} catch (TJYXException e) {
			e.printStackTrace();
			write2View(e.getMessage(), response);
		}
		return NO_DISPATCH;
	}

	/**
	 * 批量删除管理员信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String deleteUserInfo(HttpServletRequest request,
			HttpServletResponse response)throws IOException {
		String idStr = request.getParameter("ids");
		String[] ids = idStr.split(",");
		try {
			userInfoDao.deleteByIds(TableNameConstants.MAINTAIN_USERINFO,
					ids);
			write2View(ids.length, response);
		} catch (TJYXException e) {
			e.printStackTrace();
			write2View(e.getMessage(), response);
		}
		return NO_DISPATCH;
	}
	
	/**
	 * 保存管理员信息
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public String addManager(HttpServletRequest request,
			HttpServletResponse response)throws IOException {
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userName);
		userInfo.setPassword(password);
		try {
			userInfoDao.saveObject(userInfo, TableNameConstants.MAINTAIN_USERINFO);
			write2View(OK,response);
		} catch (TJYXException e) {
			e.printStackTrace();
			write2View(e.getMessage(), response);
		}
		return NO_DISPATCH;
	}
	
	/**
	 * 更新管理员信息
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public String updateManager(HttpServletRequest request,
			HttpServletResponse response)throws IOException {
		UserInfo userInfo = DTOUtil.createDTOFromRequest(UserInfo.class, request);
		try {
			userInfoDao.updateObject(userInfo, TableNameConstants.MAINTAIN_USERINFO);
			write2View(OK,response);
		} catch (TJYXException e) {
			e.printStackTrace();
			write2View(e.getMessage(),response);
		}
		return NO_DISPATCH;
	}
	
	/**
	 * 根据ID返回管理员查看窗口的信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public String viewManagerById(HttpServletRequest request,
			HttpServletResponse response)throws IOException {
		Long id = Long.parseLong(request.getParameter("id"));
		try {
			UserInfo userInfo = userInfoDao.selectObjectById(UserInfo.class, TableNameConstants.MAINTAIN_USERINFO, id);
			JSONObject json = JSONObject.fromObject(userInfo);
			write2View(json.toString(),response);
		} catch (TJYXException e) {
			e.printStackTrace();
		}
		return NO_DISPATCH;
	}
	
	/**
	 * 根据ID返回管理员更新窗口的信息
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public String updateManagerById(HttpServletRequest request,
			HttpServletResponse response)throws IOException {
		Long id = Long.parseLong(request.getParameter("id"));
		try {
			UserInfo userInfo = userInfoDao.selectObjectById(UserInfo.class, TableNameConstants.MAINTAIN_USERINFO, id);
			JSONObject json = JSONObject.fromObject(userInfo);
			write2View(json.toString(),response);
		} catch (TJYXException e) {
			e.printStackTrace();
		}
		return NO_DISPATCH;
	}
	
	/**
	 * 跳转异常页面测试
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public String errorTest(HttpServletRequest request,
			HttpServletResponse response)throws IOException {
		request.setAttribute("errMsg", "测试!~测试!~测试!~");
		return ERROR_PAGE;
	}
	
}
